package com.example.utils;

import com.example.models.data.ResponseData;
import com.example.models.data.Study.StudyListCourseData;
import com.example.models.data.Study.StudyListTryData;
import com.example.models.data.Study.StudyRecyclerviewData;
import com.example.models.data.Study.StudyTopicData;
import com.example.network.RetrofitHelper;

import java.util.ArrayList;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 董建业  时间:2017/4/13.
 */

public class StudyUtils {


    private StudyListTryData listTryData;
    private StudyListCourseData listCourse;
    private StudyTopicData listtopic;
    private boolean isstudy = false;
    public static StudyUtils studyUtils;

    private ArrayList<StudyRecyclerviewData> listdata = new ArrayList<>();
    private ArrayList<StudyRecyclerviewData> returnlistdata = new ArrayList<>();
    private InternetData internetdata;

    public interface InternetData {
        void data(ArrayList<StudyRecyclerviewData> listdata);

        void returndata(ArrayList<StudyRecyclerviewData> listdata);
    }

    public void setInternetdata(InternetData internetdata) {
        setInternetdata(internetdata, false);
    }

    public void setInternetdata(InternetData internetdata, boolean isstudy) {
        this.internetdata = internetdata;
        this.isstudy = isstudy;
    }

    public StudyUtils() {
        studyUtils = this;

        if (listdata.size() > 0)
            listdata.clear();
    }

    public void getlisttrydata() {
        Map<String, String> params = RetrofitHelper.createParams();
        params.put("page_size", "6");
        params.put("page_index", "0");
        RetrofitHelper.getUserApi().getlisttry(RetrofitHelper.getParams(params))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseData<StudyListTryData>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseData<StudyListTryData> firstHandResponseData) {
                        listTryData = firstHandResponseData.data;
                        Sortingtradata();
                    }
                });
    }

    public void getlistcoursedata() {
        Map<String, String> params = RetrofitHelper.createParams();
        params.put("page_size", "6");
        params.put("page_index", "0");
        RetrofitHelper.getUserApi().getlistcoures(RetrofitHelper.getParams(params))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseData<StudyListCourseData>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseData<StudyListCourseData> firstHandResponseData) {
                        listCourse = firstHandResponseData.data;
                        Sortingcousredata();
                    }
                });
    }

    public void getlisttoppic() {
        Map<String, String> params = RetrofitHelper.createParams();
        params.put("page_size", "6");
        params.put("page_index", "0");
        RetrofitHelper.getUserApi().getlisttopic(RetrofitHelper.getParams(params))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseData<StudyTopicData>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseData<StudyTopicData> firstHandResponseData) {
                        listtopic = firstHandResponseData.data;
                        Sortingtopicdata();
                    }
                });
    }

    public void Sortingtradata() {
        listdata.add(new StudyRecyclerviewData("0"));
        listdata.add(new StudyRecyclerviewData("试听课程"));
        for (int i = 0; i < 12; i++) {
            String object_id = listTryData.tryX.get(i % 2).object_id;
            String title = listTryData.tryX.get(i % 2).title;
            String title2 = listTryData.tryX.get(i % 2).title2;
            String speaker = listTryData.tryX.get(i % 2).speaker;
            String image = listTryData.tryX.get(i % 2).image;
            int length = listTryData.tryX.get(i % 2).length;
            int type = listTryData.tryX.get(i % 2).type;

            returnlistdata.add(new StudyRecyclerviewData(object_id, title, title2, speaker, type, image, length));
            if (i < 3)
                listdata.add(new StudyRecyclerviewData(object_id, title, title2, speaker, type, image, length));

        }
        if (isstudy)
            getlistcoursedata();
        else
            internetdata.returndata(returnlistdata);

    }

    public void Sortingcousredata() {
        listdata.add(new StudyRecyclerviewData("精选课程"));
        for (int i = 0; i < 12; i++) {
            String object_id = listCourse.course.get(i % 2).object_id;
            String title = listCourse.course.get(i % 2).title;
            String title2 = listCourse.course.get(i % 2).title2;
            String speaker = listCourse.course.get(i % 2).speaker;
            String image = listCourse.course.get(i % 2).image;
            int length = listCourse.course.get(i % 2).length;
            int type = listCourse.course.get(i % 2).type;
            int price = listCourse.course.get(i % 2).price;
            int volume = listCourse.course.get(i % 2).volume;

            returnlistdata.add(new StudyRecyclerviewData(object_id, title, title2, speaker, type, image, length, price, volume));

            if (i < 3)
                listdata.add(new StudyRecyclerviewData(object_id, title, title2, speaker, type, image, length, price, volume));
        }

        if (isstudy)
            getlisttoppic();
        else
            internetdata.returndata(returnlistdata);
    }

    public void Sortingtopicdata() {

        listdata.add(new StudyRecyclerviewData("专辑课程"));
        for (int i = 0; i < 12; i++) {
            String object_id = listtopic.topic.get(i % 2).object_id;
            String title = listtopic.topic.get(i % 2).title;
            String title2 = listtopic.topic.get(i % 2).title2;
            int type = listtopic.topic.get(i % 2).type;
            String image = listtopic.topic.get(i % 2).image;
            int length = listtopic.topic.get(i % 2).length;

            returnlistdata.add(new StudyRecyclerviewData(object_id, title, title2, type, image, length));
            if (i < 6)
                listdata.add(new StudyRecyclerviewData(object_id, title, title2, type, image, length));
        }
        if (isstudy) {
            internetdata.data(listdata);
            returnlistdata.clear();
            returnlistdata = null;
        } else {
            internetdata.returndata(returnlistdata);
            listdata.clear();
            listdata = null;
        }

    }
}
